package com.qitianyong.sgkusbcamera;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.qitianyong.adapter.GKAdapter;
import com.qitianyong.selfclass.BaseActivity;
import com.qitianyong.selfclass.CONSTANT;
import com.qitianyong.selfclass.ItemInformation;
import com.qitianyong.selfclass.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class ItemActivity extends BaseActivity {

    private TextView __tv_title;
    private ListView __lvItems;
    private GKAdapter __adapter;
    private Button __btn_back;

    private int __current_item = 0;
    private int __config_type = CONSTANT.ITEM_TYPE.TYPE_RESOLUTION;
    private List<ItemInformation> __list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item);

        findView();
        setListener();

        initView();
    }

    private void findView(){

        __tv_title = (TextView)findViewById(R.id.tv_title);
        __lvItems = (ListView)findViewById(R.id.lv_items);
        __btn_back = (Button)findViewById(R.id.btn_back);
    }

    private void setListener(){

        __btn_back.setOnClickListener(mListener);
        __lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(__current_item != position) {

                    ItemInformation info = __list.get(__current_item);
                    info.__status = false;

                    info = __list.get(position);
                    info.__status = true;

                    __adapter.notifyDataSetChanged();
                    __current_item = position;

                    // test:关闭
                    Intent intent =new Intent(ItemActivity.this, ConfigActivity.class);
                    intent.putExtra("item", __current_item);
                    setResult(__config_type, intent);

                    finish();
                }
            }
        });
    }

    private void initView(){

        __config_type = getIntent().getIntExtra("type", 0);
        __current_item = getIntent().getIntExtra("index", 0);

        String[] items_array = null;
        switch (__config_type){
            case CONSTANT.ITEM_TYPE.TYPE_RESOLUTION:{
                __tv_title.setText(getString(R.string.text_resolution));
                items_array = getResources().getStringArray(R.array.resolution_array);
                break;
            }
            case CONSTANT.ITEM_TYPE.TYPE_RECORDTIME:{
                __tv_title.setText(getString(R.string.text_recordtime));
                items_array = getResources().getStringArray(R.array.record_time_array);
                break;
            }
            case CONSTANT.ITEM_TYPE.TYPE_SENSOR:{
                __tv_title.setText(getString(R.string.text_sensor));
                items_array = getResources().getStringArray(R.array.sensor_array);
                break;
            }
            default:{
                return;
            }
        }

        for(int i=0; i<items_array.length; i++){
            ItemInformation info = new ItemInformation();

            info.__content = items_array[i];
            if(__current_item == i){
                info.__status = true;
            }else{
                info.__status = false;
            }

            __list.add(info);
        }

        __adapter = new GKAdapter(this, __list);
        __lvItems.setAdapter(__adapter);
    }

    private View.OnClickListener mListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LogUtils.addClickLog(v.getId());
            switch (v.getId()){
                case R.id.btn_back:{
                    finish();
                    break;
                }
                default:break;
            }
        }
    };
}
